﻿using HCQ2_Common;
using HCQ2_IBLL;
using HCQ2_Model;
using HCQ2_Model.VRDD.InputDto._0602;
using HCQ2_Model.VRDD.InputDto._0730;
using HCQ2_Model.VRDD.InputDto._0806;
using HCQ2_Model.VRDD.InputDto._0818;
using HCQ2_Model.VRDD.OutputDto._0602;
using HCQ2_Model.VRDD.OutputDto._0730;
using HCQ2_Model.VRDD.OutputDto._0806;
using HCQ2_Model.VRDD.OutputDto._0818;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace HCQ2_BLL
{
    public partial class T_VRDDScanLoginBLL: IT_VRDDScanLoginBLL
    {
        /// <summary>
        ///  获取用户的扫码登录列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public List<OutputUserScan> GetUserScanLoginList(InputUserScan param)
        {
            return DBSession.IT_VRDDScanLoginDAL.GetUserScanLoginList(param);
        }

        /// <summary>
        ///  获取用户体验主表记录
        /// </summary>
        /// <param name="user_id">用户id</param>
        /// <param name="file_id">作品id</param>
        /// <returns></returns>
        public OutputVRRecord GetOutputVRRecord(int user_id, int file_id)
        {
            OutputVRRecord list = new OutputVRRecord();
            VRRecordMainResult o = DBSession.IT_VRDDScanLoginDAL.GetRecordMainResult(user_id,file_id);
            if (o == null)
                return list;
            //主表
            OutputVRRecord temp = new OutputVRRecord
            {
                folder_id = o.folder_id,
                folder_name = o.folder_name,
                file_id = o.file_id,
                file_name = o.file_name,
                file_note = o.file_note,
                trial_num = o.trial_num,
                em_time = o.em_time,
                total_score = o.total_score,
                result_ok = o.result_ok,
                result_error = o.result_error
            };
            //体验记录列表
            List<VRRecord> trial_list = new List<VRRecord>();
            List<VRRecordResult> obj = DBSession.IT_VRDDScanLoginDAL.GetRecordResult(user_id, o.file_id);
            //体验记录列表
            obj?.ForEach(n =>
            {
                VRRecord rj = n.ToVRRecord();
                //成绩列表
                List<VRRecordScore> sj = new List<VRRecordScore>();
                List<VRRecordScoreResult> sn = DBSession.IT_VRDDScanLoginDAL.GetRecordScoreResult(n.er_id);
                //答题列表
                sn?.ForEach(m => {
                    VRRecordScore sjTemp = m.ToVRRecordScore();
                    //答题列表
                    List<VRRecordAnswerResult> an = DBSession.IT_VRDDScanLoginDAL.GetRecordAnswerResult(user_id, n.er_id, m.topic_num);
                    sjTemp.answer_list = an;
                    sj.Add(sjTemp);
                });
                rj.score_list = sj;
                trial_list.Add(rj);
            });
            temp.trial_list = trial_list;
            //正确率
            if (o.result_ok > 0 && o.result_error <= 0)
                temp.record_rate = 100;
            else if (o.result_ok <= 0)
                temp.record_rate = 0;
            else if (o.result_error <= 0)
                temp.record_rate = 100;
            else
            {
                //计算
                decimal all = Helper.ToDecimal(o.result_ok + o.result_error);
                temp.record_rate = Math.Round(o.result_ok / all, 2) * 100;
            }
            //缩略图 
            List<T_DocBagFileRecord> photoOption = DBSession.IT_DocBagFileRecordDAL.Select(s => s.file_id == o.file_id);
            if (photoOption.Count > 0)
                photoOption.ForEach(s => s.file_url = s.file_url.Replace("~", HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.IndexOf("/api/"))));
            temp.photoOption = photoOption;
            list = temp;
            return list;
        }
        /// <summary>
        ///  获取体验记录
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public OutputRecordCourse GetRecordCourse(InputVRWork param)
        {
            OutputRecordCourse list = new OutputRecordCourse();
            List<RecordCourse> obj = DBSession.IT_VRDDScanLoginDAL.GetRecordCourse(param);
            if (obj == null || obj.Count <= 0)
                return list;
            obj?.ForEach(o => {
                OutputRecordCourse temp = o.ToOutputRecordCourse();
                //成绩列表
                List<RecordScoreListResult> sList = DBSession.IT_VRDDScanLoginDAL.GetRecordCourse(o.folder_path,param.keyword);
                List<RecordScoreList> score_list = new List<RecordScoreList>();
                sList?.ForEach(n => {
                    RecordScoreList sTemp = n.ToRecordScoreList();
                    //作品缩略图
                    List<T_DocBagFileRecord> photoOption = DBSession.IT_DocBagFileRecordDAL.Select(s => s.file_id == n.file_id);
                    if (photoOption.Count > 0)
                        photoOption.ForEach(s => s.file_url = s.file_url.Replace("~", HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.IndexOf("/api/"))));
                    sTemp.photoOption = photoOption;
                    //正确率
                    if (n.result_ok > 0 && n.result_error <= 0)
                        sTemp.record_rate = 100;
                    else if (n.result_ok <= 0)
                        sTemp.record_rate = 0;
                    else if (n.result_error <= 0)
                        sTemp.record_rate = 100;
                    else
                    {
                        //计算
                        decimal all = Helper.ToDecimal(n.result_ok + n.result_error);
                        sTemp.record_rate = Math.Round(n.result_ok / all, 2) * 100;
                    }
                    score_list.Add(sTemp);
                });
                temp.score_list = score_list;
                //课程缩略图【封面】
                if (!string.IsNullOrEmpty(o.folder_url))
                    temp.folder_url = o.folder_url?.Replace("~", HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.IndexOf("/api/")));
                //正确率
                if (o.ok_num > 0 && o.error_num <= 0)
                    temp.record_rate = 100;
                else if (o.ok_num <= 0)
                    temp.record_rate = 0;
                else if (o.error_num <= 0)
                    temp.record_rate = 100;
                else
                {
                    //计算
                    decimal all = Helper.ToDecimal(o.ok_num + o.error_num);
                    temp.record_rate = Math.Round(o.ok_num / all, 2) * 100;
                }
                list = temp;
            });
            return list;
        }
        /// <summary>
        ///  获取服务类别信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>

        public List<OutputService> GetServiceType(InputService param)
        {
            return DBSession.IT_VRDDScanLoginDAL.GetServiceType(param);
        }
        /// <summary>
        /// 获取店铺目录列表
        /// </summary>
        /// <param name="param"></param>
        /// <param name="user_id"></param>
        /// <returns></returns>
        public List<OutputStorePage> GetStorePageList(InputStorePage param, int user_id)
        {
            var list = DBSession.IT_VRDDScanLoginDAL.GetStorePageList(param, user_id);
            list?.ForEach(s =>
            {
                if(!string.IsNullOrEmpty(s.Icon))
                    s.Icon= s.Icon.Replace("~", HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.IndexOf("/api/")));
                //协议地址
                if (s.dealId !=null)
                    s.dealUrl = HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.IndexOf("/api/")) + $"/uploadFile/FouderPact/FouderPact.html?id={s.dealId}";
            });
            return list;
        }
    }
}